#define _CRT_SECURE_NO_WARNINGS   1

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

struct treeNode
{
	long long val;
	treeNode* left;
	treeNode* right;
	treeNode(long long v) :val(v), left(nullptr), right(nullptr){}
};

long long res = 0;
class Compare_Node_Pointer
{
public:
	bool operator () (treeNode* &a, treeNode* &b) const
	{
		return a->val > b->val;
	}
};

void dfs(treeNode* root, int dep)
{
	if (root&&!root->left)
	{
		res += dep*root->val;
		return;
	}

	dfs(root->left, dep + 1);
	dfs(root->right, dep + 1);
}


int main()
{
	int n;
	cin >> n;
	vector<int> cnts(n);
	priority_queue<treeNode*, vector<treeNode*>, Compare_Node_Pointer> que;
	for (int i = 0; i<n; i++)
	{
		cin >> cnts[i];
		que.push(new treeNode(cnts[i]));
	}
	while (que.size()>1)
	{
		treeNode *l = que.top();
		que.pop();
		treeNode *r = que.top();
		que.pop();
		treeNode *root = new treeNode(l->val + r->val);
		root->left = l;
		root->right = r;
		que.push(root);
	}
	dfs(que.top(), 0);
	cout << res << endl;
	return 0;
}